<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>AceCRC: /home/brian/src/AceCRC/src/ace_crc/crc16modbus_byte.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">AceCRC
   &#160;<span id="projectnumber">1.1.1</span>
   </div>
   <div id="projectbrief">Cyclic Redundancy Check (CRC) algorithms (e.g. crc16ccitt, crc32) programmatically converted from C99 code generated by pycrc (https://pycrc.org) to Arduino C++ using namespaces and PROGMEM flash memory.</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_68267d1309a1af8e8297ef4c3efbcdba.html">src</a></li><li class="navelem"><a class="el" href="dir_7e2fca1243a23cea849d389c30292e19.html">ace_crc</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">crc16modbus_byte.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<a href="crc16modbus__byte_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160; </div>
<div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;<span class="preprocessor">#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_SAMD)</span></div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="preprocessor">  #include &lt;avr/pgmspace.h&gt;</span></div>
<div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;<span class="preprocessor">#else</span></div>
<div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;<span class="preprocessor">  #include &lt;pgmspace.h&gt;</span></div>
<div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;<span class="preprocessor">#endif</span></div>
<div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="crc16modbus__byte_8hpp.html">crc16modbus_byte.hpp</a>&quot;</span> <span class="comment">// header file converted by AceCRC</span></div>
<div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;<span class="preprocessor">#include &lt;stdlib.h&gt;</span></div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;<span class="preprocessor">#include &lt;stdint.h&gt;</span></div>
<div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160; </div>
<div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;<span class="keyword">namespace </span>ace_crc {</div>
<div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;<span class="keyword">namespace </span>crc16modbus_byte {</div>
<div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160; </div>
<div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160; </div>
<div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160; </div>
<div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;<span class="keyword">static</span> <span class="keyword">const</span> <a class="code" href="crc16ccitt__bit_8hpp.html#a5a452c1a176f3899bffa9a34fc6143bf">crc_t</a> crc_table[256] PROGMEM = {</div>
<div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;    0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,</div>
<div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;    0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,</div>
<div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;    0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,</div>
<div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;    0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,</div>
<div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;    0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,</div>
<div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;    0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,</div>
<div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;    0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,</div>
<div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;    0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,</div>
<div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;    0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,</div>
<div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;    0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,</div>
<div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;    0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,</div>
<div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;    0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,</div>
<div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;    0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,</div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;    0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,</div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;    0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,</div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;    0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,</div>
<div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;    0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,</div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;    0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,</div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;    0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,</div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;    0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,</div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;    0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;    0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,</div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;    0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,</div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;    0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,</div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;    0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,</div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;    0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,</div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;    0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,</div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;    0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,</div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;    0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,</div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;    0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,</div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;    0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,</div>
<div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;    0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;};</div>
<div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160; </div>
<div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160; </div>
<div class="line"><a name="l00073"></a><span class="lineno"><a class="line" href="crc16modbus__byte_8cpp.html#ab8c9a50ddc05954e759f4df0aae0388a">   73</a></span>&#160;<a class="code" href="crc16modbus__byte_8hpp.html#adb9df98f1aa260582377d5710319543c">crc_t</a> crc_reflect(<a class="code" href="crc16modbus__byte_8hpp.html#adb9df98f1aa260582377d5710319543c">crc_t</a> data, <span class="keywordtype">size_t</span> data_len)</div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;{</div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;    <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;    <a class="code" href="crc16modbus__byte_8hpp.html#adb9df98f1aa260582377d5710319543c">crc_t</a> ret;</div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160; </div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;    ret = data &amp; 0x01;</div>
<div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;    <span class="keywordflow">for</span> (i = 1; i &lt; data_len; i++) {</div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;        data &gt;&gt;= 1;</div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;        ret = (ret &lt;&lt; 1) | (data &amp; 0x01);</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;    }</div>
<div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;    <span class="keywordflow">return</span> ret;</div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;}</div>
<div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160; </div>
<div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160; </div>
<div class="line"><a name="l00087"></a><span class="lineno"><a class="line" href="crc16modbus__byte_8cpp.html#ab0462dd845758558360c343ed4e11e6e">   87</a></span>&#160;<a class="code" href="crc16modbus__byte_8hpp.html#adb9df98f1aa260582377d5710319543c">crc_t</a> crc_update(<a class="code" href="crc16modbus__byte_8hpp.html#adb9df98f1aa260582377d5710319543c">crc_t</a> crc, <span class="keyword">const</span> <span class="keywordtype">void</span> *data, <span class="keywordtype">size_t</span> data_len)</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;{</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;    <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *d = (<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *)data;</div>
<div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;    uint8_t tbl_idx;</div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160; </div>
<div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;    <span class="keywordflow">while</span> (data_len--) {</div>
<div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;        tbl_idx = (crc ^ *d) &amp; 0xff;</div>
<div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;        crc = ((pgm_read_word(crc_table + (tbl_idx))) ^ (crc &gt;&gt; 8)) &amp; 0xffff;</div>
<div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;        d++;</div>
<div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160;    }</div>
<div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;    <span class="keywordflow">return</span> crc &amp; 0xffff;</div>
<div class="line"><a name="l00098"></a><span class="lineno">   98</span>&#160;}</div>
<div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160; </div>
<div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;} <span class="comment">// crc16modbus_byte</span></div>
<div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;} <span class="comment">// ace_crc</span></div>
<div class="ttc" id="acrc16ccitt__bit_8hpp_html_a5a452c1a176f3899bffa9a34fc6143bf"><div class="ttname"><a href="crc16ccitt__bit_8hpp.html#a5a452c1a176f3899bffa9a34fc6143bf">ace_crc::crc16ccitt_bit::crc_t</a></div><div class="ttdeci">uint16_t crc_t</div><div class="ttdoc">The type of the CRC values.</div><div class="ttdef"><b>Definition:</b> <a href="crc16ccitt__bit_8hpp_source.html#l00068">crc16ccitt_bit.hpp:68</a></div></div>
<div class="ttc" id="acrc16modbus__byte_8hpp_html"><div class="ttname"><a href="crc16modbus__byte_8hpp.html">crc16modbus_byte.hpp</a></div><div class="ttdoc">Functions and types for CRC checks.</div></div>
<div class="ttc" id="acrc16modbus__byte_8hpp_html_adb9df98f1aa260582377d5710319543c"><div class="ttname"><a href="crc16modbus__byte_8hpp.html#adb9df98f1aa260582377d5710319543c">ace_crc::crc16modbus_byte::crc_t</a></div><div class="ttdeci">uint16_t crc_t</div><div class="ttdoc">The type of the CRC values.</div><div class="ttdef"><b>Definition:</b> <a href="crc16modbus__byte_8hpp_source.html#l00068">crc16modbus_byte.hpp:68</a></div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.1
</small></address>
</body>
</html>
